<?php

declare(strict_types=1);

namespace app\model;

use think\Model;
use think\model\concern\SoftDelete;

class Org extends Model
{
    use SoftDelete;

    protected $name = 'org';

    protected $autoWriteTimestamp = 'datetime';

    protected $json = ['extra'];

    public const TYPE_SELLER = 'seller';
    public const TYPE_BUYER = 'buyer';
    public const TYPE_BOTH = 'both';

    public const STATUS_PENDING = 0;
    public const STATUS_ACTIVE = 1;
    public const STATUS_DISABLED = 2;

    public function profile()
    {
        return $this->hasOne(OrgProfile::class, 'org_id');
    }

    public function contact()
    {
        return $this->hasOne(OrgContact::class, 'org_id');
    }

    public function addresses()
    {
        return $this->hasMany(OrgAddress::class, 'org_id');
    }

    public function members()
    {
        return $this->hasMany(OrgMember::class, 'org_id');
    }

    public function users()
    {
        return $this->belongsToMany(User::class, 'org_member', 'user_id', 'org_id');
    }

    public function region()
    {
        return $this->belongsTo(Region::class, 'region_id');
    }
}
